<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1.冒泡排序</title>
</head>
<body>
    
</body>
<script>
    // let array = [456465,123,5412,132,124,123,124,1231,5412,312]

    // let bubleSort = function (array) {
    //     let length = array.length
    //     console.log('length:', length)
    //     for(let i = 1; i < length; i++){
    //         for(let j = 0; j < length - i ; j++ ){
    //             if(array[j] > array[j+1]){
    //                 let temp = array[j]
    //                 array[j] = array[j+1]
    //                 array[j+1] = temp
    //             }
    //         }
    //     }
    // }
    
    // bubleSort(array)
    // console.log("排序后的结果:", array)

    let array = [2,31,21,441,2,41,23,1,1,0,2,14,1,21]

    let bubleSort = function(array) {
        let arrayLength = array.length
        for(let i = 1; i < arrayLength; i++) {
            let hasSort = true
            for(let j = 0;j < arrayLength-i; j++) {            
                if(array[j] > array[j+1]) {
                    let temp = array[j]
                    array[j] = array[j+1]
                    array[j+1] = temp                    
                    hasSort = false
                }
            }
            if(hasSort) {
                break
            }
        }
    }

    bubleSort(array)
    console.log('array排序之后:', array)

</script>
</html> 